// api/ai_role_api/ai_role_recent_session.go
package ai_role_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (AiRoleApi) AiRoleRecentSession(c *gin.Context) {
	var cr models.IDRequest
	err := c.ShouldBindQuery(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	// 查这个角色最新的会话列表
	var role models.AiRoleModel
	err = global.DB.Take(&role, cr.ID).Error
	if err != nil {
		res.FailWithMsg(c, "角色不存在")
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)
	var recentSession models.SessionModel
	err = global.DB.
		Order("created_at desc").
		Take(&recentSession, "role_id = ? and user_id = ?", cr.ID, claims.UserID).Error
	if err != nil {
		res.OkWithData(c, 0)
		return
	}
	res.OkWithData(c, recentSession.ID)
	return
}
